package net.emsquared.gamedev.entity;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import net.emsquared.gamedev.TestGame;
import net.emsquared.gamedev.maps.MapCoordinates;
import net.emsquared.gamedev.screens.IScreen;

/**
 * Code is not copyrighted...I don't think.
 *
 * @author Matt Meza (mmeza84@gmail.com)
 */

public abstract class Entity extends Sprite
{
   private MapCoordinates position;
   private IScreen screen;

   private float currentPosition;

   /**
    * This class is an abstract "drawable" in-game object. Used for playable characters, enemies, items, whatever.
    * @param aTexturePath path to the image
    */
   public Entity(String aTexturePath, IScreen aScreen)
   {
      super(new Texture(Gdx.files.internal(aTexturePath)));
      screen = aScreen;
   }

   /**
    * This class is an abstract "drawable" in-game object. Used for playable characters, enemies, items, whatever.
    * @param aTexturePath path to the image
    * @param x tile position on the x-axis
    * @param y tile position on the y-axis
    */
   public Entity(String aTexturePath, IScreen aScreen, int x, int y)
   {
      this(aTexturePath, aScreen);
      position = new MapCoordinates(x, y);
   }

   /**
    * Returns tile index on the x-axis
    * @return tile position on the x-axis
    */
   public int getXPos_Tile()
   {
      return position.getX();
   }

   public void setXPos_Tile(int aXPos)
   {
      position.setX(aXPos);
   }

   /**
    * Returns tile index on the y-axis
    * @return tile position on the y-axis
    */
   public int getYPos_Tile()
   {
      return position.getY();
   }

   public void setYPos_Tile(int aYPos)
   {
      position.setY(aYPos);
   }

   /**
    * This returns the bottom_left of the tile in question (1, 1) centers at (32px, 32px).
    * This assume a 32x32 tile system. Will need to refactor this for future endeavors.
    * @return xpos of bottom left of entity;
    */
   public float getXPos_Pix()
   {
      return position.getX() * 32;
   }

   /**
    * This returns the bottom_left of the tile in question (1, 1) centers at (32px, 32px).
    * This assume a 32x32 tile system. Will need to refactor this for future endeavors.
    * @return ypos of bottom left of entity;
    */
   public float getYPos_Pix()
   {
      return position.getY() * 32;
   }

   /**
    * Overridden bounding box for sprite...override this to make custom boxes.
    * @return Rectangle bounding box for entity
    */
   public Rectangle getBoundingRectangle()
   {
      return new Rectangle(position.getX(), position.getY(), getWidth(), getHeight());
   }

   /**
    * Overridden collision check. Couldn't get built-in collisions working for some reason.
    * @param aCollidableEntity item we're colliding with
    * @return boolean whether or not we collided
    */
   public boolean collidesWith(Entity aCollidableEntity)
   {
      return getBoundingRectangle().overlaps(aCollidableEntity.getBoundingRectangle());
   }

   /**
    * Overridden draw method using the tile index functionality
    * @param spriteBatch SpriteBatch to draw on
    */
   @Override
   public void draw(SpriteBatch spriteBatch)
   {
      spriteBatch.draw(this, getXPos_Pix(), getYPos_Pix(), getWidth(), getHeight());
   }

   public MapCoordinates getMapCoordinates()
   {
      return position;
   }

   public IScreen getScreen()
   {
      return screen;
   }
}
